package com.feihong.yw189.app;

import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.multidex.MultiDex;
import android.support.v7.app.AppCompatDelegate;
import android.util.Log;

import com.blankj.utilcode.util.Utils;
import com.feihong.yw189.bluetooth.utils.util.ToastUtils;
import com.feihong.yw189.http.HttpTools;
import com.inuker.bluetooth.library.BluetoothClient;
import com.inuker.bluetooth.library.BluetoothContext;
import com.meiqia.core.callback.OnInitCallback;
import com.meiqia.meiqiasdk.util.MQConfig;
import com.pddstudio.preferences.encrypted.EncryptedPreferences;
import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.tinker.entry.DefaultApplicationLike;
import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaomi.mistatistic.sdk.MiStatInterface;

import org.xutils.x;

import java.util.List;

/**
 * Created by Sabirjan on 2017/11/13.
 * DX189.
 * 飞宏电子
 */

public class App extends DefaultApplicationLike {
    /**
     * 小米推送appid  appkey
     */
    private static final String APP_ID = "2882303761517693511";
    private static final String APP_KEY = "5241769387511";

    private String mUserAccout, mUserPassword;

    public App(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
        super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
    }

    public void setUserAccount(String account, String password) {
        mUserAccout = account;
        mUserPassword = password;
    }

    public String getUserPassword() {
        return mUserPassword;
    }

    public String getUserAccount() {
        return mUserAccout;
    }

    /**
     * 微信支付appid
     */
    public static final String WECHAT_PAY_APPID = "wx2f0d2647197c2d89";
    private static final String KEY_MEIQIA_KEFU = "73cadbae79f5d3c5c84265f3b6fd9b35";
    //公钥

    private HttpTools httpTools;

    public HttpTools getHttpTools() {
        return httpTools;
    }

    public EncryptedPreferences getEncryptedPreferences() {
        return encryptedPreferences;
    }

    private EncryptedPreferences encryptedPreferences = null;
    private static App app = null;

    /**
     * 唯一标识符，一般推送的ID来初始化
     */
    public String REGID = "";

    private static final String CHANNEL = "官网";

    public static App getApp() {
        return app;
    }

    public long lastUpdateToken = 0;

    @Override
    public void onCreate() {
        super.onCreate();

        // 设置当前包为开发者设备包
        Bugly.setIsDevelopmentDevice(getApplication(),false);
        // 初始化Tinker  第二个参数为APPID,第三个参数判断当前是否是Debug模式,签名时改为false
        Bugly.init(getApplication(), "40e485fd09", false);

        BluetoothContext.set(getApplication());
        //初始化蓝牙
        app = this;

        initUtils();

        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

    }

    public static void showToast(String message) {
        ToastUtils.showShortToast(app.getApplication(), message);
    }

    private void initUtils() {

        initPush();

        httpTools = new HttpTools();
        //初始化工具类
        Utils.init(getApplication());
        //初始化网络
        x.Ext.init(getApplication());
        //初始化加密配置文件
        encryptedPreferences = new EncryptedPreferences.Builder(getApplication()).withEncryptionPassword("DX189FhDz").build();

        //小米移动统计
        MiStatInterface.initialize(getApplication(), APP_ID, APP_KEY, CHANNEL);

        //美洽
        MQConfig.init(getApplication(), KEY_MEIQIA_KEFU, new OnInitCallback() {
            @Override
            public void onSuccess(String clientId) {
                Log.d("美洽客服", "onSuccess() called with: clientId = [" + clientId + "]");

            }

            @Override
            public void onFailure(int code, String message) {
                Log.d("美洽客服", "onFailure() called with: code = [" + code + "], message = [" + message + "]");
            }
        });
    }
    @Override
    public void onBaseContextAttached(Context base) {
        super.onBaseContextAttached(base);
        MultiDex.install(getApplication());
        // 安装Tinker
        Beta.installTinker(this);
    }
    public void initPush() {
        // 注册push服，注册成功后会向DemoMessageReceiver发送广播
        // 可以从DemoMessageReceiver的onCommandResult方法中MiPushCommandMessage对象参数中获取注册信息
        if (shouldInit()) {
            MiPushClient.registerPush(getApplication(), APP_ID, APP_KEY);
        }
    }

    private boolean shouldInit() {
        ActivityManager am = ((ActivityManager) getApplication().getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
        String mainProcessName = getApplication().getPackageName();
        int myPid = android.os.Process.myPid();
        for (ActivityManager.RunningAppProcessInfo info : processInfos) {
            if (info.pid == myPid && mainProcessName.equals(info.processName)) {
                return true;
            }
        }
        return false;
    }

    private BluetoothClient mClinet = null;

    public BluetoothClient getmClinet() {
        if (mClinet == null) {
            mClinet = new BluetoothClient(getApplication());
        }
        return mClinet;
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void registerActivityLifecycleCallback(Application.ActivityLifecycleCallbacks callbacks) {
        getApplication().registerActivityLifecycleCallbacks(callbacks);
    }
    /**
     * 1) 冗余日志、吐司
     * 2) 无网络状态界面
     * 3) 界面优化
     * 4) 补丁
     * 5) web页面title
     * 6) 补卡提交订单梳理
     * 7) 统一上拉加载下拉刷新框架
     * 8) 适配全面屏
     * 9) 适配NOP系统
     *
     */
}
